using System;
using System.Collections.Generic;
using System.Text;
using ALib;
using TradingLab.Data;

namespace DotNetCom
{
    public class StringCaller:IStringCaller
    {
        public void Compute(string input, out string output)
        {
            string[] record = input.Split(new char[] {','});
            output = GetCalcCommandForNic(record);
        }

        public static string GetCalcCommandForNic(string[] record)
        {
            DateTime time = ALib.DateTimeConverter.ToDateTime(record[0]);
            TimeData timeData = new TimeData();
            for (int i = 1; i < record.Length; i++)
            {
                string[] tickerData = record[i].Split(new char[] { '|' });
                timeData.Add(tickerData[0].Replace("_", ","),
                             record[0]
                             , tickerData[1] == string.Empty ? double.NaN : double.Parse(tickerData[1]) // Open
                             , tickerData[2] == string.Empty ? double.NaN : double.Parse(tickerData[2]) // High
                             , tickerData[3] == string.Empty ? double.NaN : double.Parse(tickerData[3]) // Low
                             , tickerData[4] == string.Empty ? double.NaN : double.Parse(tickerData[4]) // Close
                             , tickerData[5] == string.Empty ? double.NaN : double.Parse(tickerData[5]) // Volume
                             , double.Parse(tickerData[6]) // existingOpenPosition
                             , double.Parse(tickerData[6 + 1]) // sessionTimePercentageLeft
                             , double.Parse(tickerData[7 + 1]) // Exchange rate
                             , double.Parse(tickerData[8 + 1]) // Point value
                             , double.Parse(tickerData[9 + 1]) // Min move
                             , double.Parse(tickerData[10 + 1]) // Lot size
                             , double.Parse(tickerData[11 + 1]) // CommissionOnRate
                             , double.Parse(tickerData[12 + 1]) // Commission per share in XXX
                             , double.Parse(tickerData[13 + 1]) // Min commission in XXX
                             , double.Parse(tickerData[14 + 1]) // Max commission in XXX
                             , double.Parse(tickerData[15 + 1]) // Stamp duty and slippage rate
                             , double.Parse(tickerData[16 + 1]) // Slippage per share in XXX
                    );
                GoodValue.Assert(tickerData.Length == 18);
            }
            string calcCommand = timeData.ToCalcCommand(time);
            return calcCommand;
        }
    }
}
